//
//  TTAudioPlayer.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/26.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit
import StreamingKit

final class TTAudioPlayer: NSObject {
    static var audioPlayer:STKAudioPlayer={
        let audioPlayer = STKAudioPlayer()
        
        return audioPlayer
    }()
    static var activeMusic:PlayListModel? = nil
    static var activeMusicArr:[PlayListModel]? = nil
    static var isSelected:Bool = false
    static var isFirstPlay:Bool = true
    static var playUrl:String?
    static var imageUrl:String?
    static var programId:Int = 9999
    static var sesionId:Int = 9999
    static var indextNum:Int = 0
    static var isIndextNum:Int = 0
    static var progressDoub:Double = 0.0
    static var create_time:String = "0"
    static var end_time:String = "0"
    static var activeMusiclist: PlayListModel? {
        didSet {
            guard let model = activeMusiclist else { return }
            activeMusic = model
            playUrl = activeMusic?.playUrl
            imageUrl = activeMusic?.imageUrl
            sesionId = (activeMusic?.id)!
        }
    }
    
    static func stop() {
        self.audioPlayer.stop()
        
    }
    
    static func progress() -> Double{
        return self.audioPlayer.progress
    }
    
    static func duration() -> Double{
        return self.audioPlayer.duration
    }
    
    static func play() {
//        self.end_time = String.getNowTime()
        self.audioPlayer.play(playUrl!)
        self.isFirstPlay = false
        HomeRequestModel.increaseHistoryAndCollections(parameters: ["userId":String.getUserId(),"seasonId":self.sesionId,"status":0,"programId":self.programId,"h5Url":""]) { (success) in
            print(success)
        }
        
        
        self.create_time = String.getNowTime()
    }
    
    static func uploadProgress() {
        let pro = self.audioPlayer.progress
        var dic = UserDefaults.standard.dictionary(forKey: PlayDictionary)
        dic!["\(self.programId)"] = pro
        UserDefaults.standard.set(dic, forKey: PlayDictionary)
    }
    
    static func uploadDate() {
        if self.create_time == "0" {
            
        } else {
            self.end_time = String.getNowTime()
            let timeLength = String.getTimeDifference(create_time: self.create_time, end_time: self.end_time)
            
            HomeRequestModel.uploadingListenDate(parameters: ["programId":self.programId, "seasonId":self.sesionId,"timeLength":timeLength,"userId":String.getUserId()])
        }
    }
    
    static func playUrl(url:String?) {
        if url == "" {
            
        } else {
            self.audioPlayer.play(url!)
            
        }
    }
    
    static func firstPlay(proId:Int, musicModel:PlayListModel) {
        self.activeMusiclist = musicModel
        guard proId == self.programId else {
            self.programId = proId
            
            self.isSelected = false
            self.isFirstPlay = true
            //            self.audioPlayer.stop()
            return
        }
    }
    
    static func resume() {
        self.create_time = String.getNowTime()
        self.audioPlayer.resume()
        self.isFirstPlay = false
        self.isSelected = true
    }
    
    static func state() -> STKAudioPlayerState {
        return self.audioPlayer.state
    }
    
    static func pause(){
        self.uploadDate()
        self.audioPlayer.pause()
        self.isFirstPlay = false
        self.isSelected = false
       
        
        self.create_time = "0"
    }
    
    static func downLoadData() {
    
    }
    
    static func playPreviousSong() {
        let indMax = self.activeMusicArr!.count - 1
        var ind = self.isIndextNum
        if ind == 0 {
            ind = indMax
        } else {
            ind = ind - 1
        }
        
        if self.activeMusicArr?[ind].buySelf == 0 && self.activeMusicArr?[ind].tryStatus == 1 {
            TTAlertView.showMessage(message: "需要先购买才能试听哦")
        } else {
            self.isSelected = true
            self.indextNum = ind
            self.isIndextNum = ind
            self.sesionId = self.activeMusicArr![self.isIndextNum].id
            self.activeMusiclist = self.activeMusicArr![self.isIndextNum]
            self.play()
            
        }
    }
    
    static func playNextSong() {
        let indMax = self.activeMusicArr!.count - 1
        var ind = self.isIndextNum
        if ind == indMax {
            ind = 0
        } else {
            ind = ind + 1
        }
        if self.activeMusicArr?[ind].buySelf == 0 && self.activeMusicArr?[ind].tryStatus == 1 {
            TTAlertView.showMessage(message: "需要先购买才能听哦")
        } else {
            self.isSelected = true
            self.indextNum = ind
            self.isIndextNum = ind
            self.sesionId = self.activeMusicArr![self.isIndextNum].id
            self.activeMusiclist = self.activeMusicArr![self.isIndextNum]
            self.play()
            
        }
    }
    
    static func activeSong()->PlayListModel?{
        return activeMusic
    }
    
}


